package ru.niir.goodfil.ui;

import ru.niir.goodfil.db.Order;
import ru.niir.goodfil.db.OrderItem;
import ru.niir.goodfil.db.StorageDeliveryStatus;
import ru.niir.goodfil.utils.SumFormat;

import java.util.*;

public class StorekeeperDeliveryReportBean extends PageBean {

    private Date date;

    private ArrayList<Long> orderIds = new ArrayList<Long>();

    private long orderId;

    private List<OrderItem> orderItemsSum = new ArrayList<OrderItem>();

    private List<Order> orderList;

    private String deliveryOrdersSum;

    public StorekeeperDeliveryReportBean() {
        init();
    }

    public List<Order> getItems(){
        List<Order> list = gf().getDeliveryOrdersByDate(getDate());
        List<Order> result = new ArrayList<Order>();
        for (Order order : list) {
            order.setClient(gf().getClientById(order.getClientId()));
            order.setManager(gf().getUserById(order.getManagerId()));
            order.setDeliveryInfoExt(gf().getClientsDeliveryInfo().getOrderDeliveryInfoById(order.getId()));
            gf().addOrderInfo(order);
            result.add(order);
        }
        return result;
    }

    public void init(){
        Calendar calendar = Calendar.getInstance();
        setDate(calendar.getTime());
        orderList = getItems();
    }

    public void addOrderId(){
        for (long l : orderIds){
            if(getOrderId() == l){
                orderIds.remove(l);
                return;
            }
        }
        orderIds.add(getOrderId());
    }

    public List<OrderItem> getOrderItemsSum() {
        if(orderIds.size() == 0) return new ArrayList<OrderItem>();
        orderItemsSum = gf().getOrderItemsSumByOrderIds(orderIds);
        for(OrderItem item : orderItemsSum){
            item.setGood(gf().getGoodById(item.getGoodId()));
        }
        Collections.sort(orderItemsSum, new Comparator<OrderItem>() {
            public int compare(OrderItem o1, OrderItem o2) {
                return o1.getGood().getCode().compareTo(o2.getGood().getCode());
            }
        });
        return orderItemsSum;
    }

    public void reloadData(){
        orderIds.clear();
        orderItemsSum.clear();
        orderId = 0;
    }

    public Date getDate() {
        if (date == null) {
            return null;
        } else {
            return new java.util.Date(date.getTime());
        }
    }

    public void setDate(Date date) {
        if (date != null) {
            this.date = new java.sql.Date(date.getTime());
        } else {
            this.date = null;
        }
    }

    public long getOrderId() {
        return orderId;
    }

    public void setOrderId(long orderId) {
        this.orderId = orderId;
    }

    public String getDeliveryOrdersSum() {
        double sum = 0;
        for(Order order : orderList){
            sum += order.getSum();
        }
        return SumFormat.formatSum("###,###,##0.00", sum);
    }
}
